package com.example.music_mediaplayer.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ListView;

import com.example.music_mediaplayer.ApiInfo;

import com.example.music_mediaplayer.R;
import com.example.music_mediaplayer.adapter.MyListViewAdapter;

import com.example.music_mediaplayer.globalvariable.GlobalVariable;
import com.example.music_mediaplayer.gson.GsonUtil;
import com.example.music_mediaplayer.model.JBase;
import com.example.music_mediaplayer.model.JsonBase;
import com.example.music_mediaplayer.service.MyService;
import com.example.music_mediaplayer.tool.LogoutT;


import com.example.music_mediaplayer.tool.ToastTools;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import okhttp3.Call;


public class WangYiFragment extends Fragment{
    private View rootView;
    private ListView listView;
    public MyListViewAdapter adapter;
    private Intent intent;
    private boolean flg = true;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (rootView == null){
            rootView = inflater.inflate(R.layout.fragment_wangyi,null);
        }
        initView(rootView);
        return rootView;
    }


    public void initView(View rootView){
        listView = (ListView) rootView.findViewById(R.id.song_list);
        adapter = new MyListViewAdapter(getActivity(), GlobalVariable.WangYiSongList,2);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, final int i, long l) {
                OkHttpUtils
                        .get()
                        .url(ApiInfo.wangYiSongUrl)
                        .addParams("type","song")
                        .addParams("id",GlobalVariable.WangYiSongList.get(i).getId())
                        .build()
                        .execute(new StringCallback() {
                            @Override
                            public void onError(Call call, Exception e, int id) {

                            }
                            @Override
                            public void onResponse(String response, int id) {
                                JsonBase jsonBase = GsonUtil.getIns().parseJsonWithGson(response,JsonBase.class);
                                intent = new Intent(getActivity(), MyService.class);
                                if (!jsonBase.getData().get(0).getUrl().toString().equals("")&&jsonBase.getData().get(0).getUrl().toString()!=null){
                                    GlobalVariable.WangYiSongList.get(i).setSongPath(jsonBase.getData().get(0).getUrl().toString());
                                    intent.putExtra("url",jsonBase.getData().get(0).getUrl().toString());
                                    intent.putExtra("songName",GlobalVariable.WangYiSongList.get(i).getSongName());
                                    intent.putExtra("songArtist",GlobalVariable.WangYiSongList.get(i).getSongArtist());
                                    intent.putExtra("songPhoto",GlobalVariable.WangYiSongList.get(i).getSongPhoto());
                                    intent.putExtra("msg","");

//                                    if (GlobalVariable.LieBiaoSongList.size()==0){
//                                        GlobalVariable.LieBiaoSongList.add(GlobalVariable.WangYiSongList.get(i));
//                                    }else{
//                                        for (int j = 0;j<GlobalVariable.LieBiaoSongList.size();j++){
//                                            if (GlobalVariable.LieBiaoSongList.get(j).getSongPath().equals(GlobalVariable.WangYiSongList.get(i).getSongPath())){
//                                                flg = true;
//                                                return;
//                                            }else{
//                                                flg = false;
//                                            }
//                                        }
//                                        if (flg == false){
//                                            GlobalVariable.LieBiaoSongList.add(GlobalVariable.WangYiSongList.get(i));
//                                        }
//                                    }

                                }else{
                                    ToastTools.toastMsg(getActivity(),"该歌曲不能播放");
                                }
                            }
                        });

                /**
                 * 获取歌词
                 */
                OkHttpUtils
                        .get()
                        .url(ApiInfo.wangYiSongLyric)
                        .addParams("type","lyric")
                        .addParams("id",GlobalVariable.WangYiSongList.get(i).getId())
                        .build()
                        .execute(new StringCallback() {
                            @Override
                            public void onError(Call call, Exception e, int id) {

                            }

                            @Override
                            public void onResponse(String response, int id) {
//                                LogoutT.logout("0101010101"+response);
                                JBase jBase = GsonUtil.getIns().parseJsonWithGson(response,JBase.class);
                                GlobalVariable.WangYiSongList.get(i).setSongLyric(jBase.getLrc().getLyric());

//                                if (GlobalVariable.LieBiaoSongList.size()==0){
//                                    GlobalVariable.LieBiaoSongList.add(GlobalVariable.WangYiSongList.get(i));
//                                }else{
//                                    for (int j = 0;j<GlobalVariable.LieBiaoSongList.size();j++){
//                                        if (GlobalVariable.LieBiaoSongList.get(j).getSongPath().equals(GlobalVariable.WangYiSongList.get(i).getSongPath())){
//                                            flg = true;
//                                            return;
//                                        }else{
//                                            flg = false;
//                                        }
//                                    }
//                                    if (flg == false){
//                                        GlobalVariable.LieBiaoSongList.add(GlobalVariable.WangYiSongList.get(i));
//                                    }
//                                }
                                if (GlobalVariable.WangYiSongList.get(i).getSongPath()!=null){
                                    if (!GlobalVariable.WangYiSongList.get(i).getSongPath().equals("")){
                                        if (!jBase.getLrc().getLyric().equals("")&&jBase.getLrc().getLyric()!=null){
                                            intent.putExtra("songLrc",jBase.getLrc().getLyric());
                                            getActivity().startService(intent);
                                        }
                                    }
                                }
                            }
                        });
            }
        });
    }


}
